﻿using EasyCore.Excel;
using NPOI.SS.Formula.Eval;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.SS.UserModel.Charts;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using Org.BouncyCastle.Bcpg.OpenPgp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Net.Cache;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore
{
    //public delegate object ColumnValFunc<T>(int rowIndex, T row);

    public class DataTableSheetMapper : SheetMapper<DataRow>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="excelMapper"></param>
        /// <param name="sheetName"></param>
        public DataTableSheetMapper(ExcelMapper excelMapper, string sheetName) : base(excelMapper, sheetName)
        {
            ExcelColumns = new List<ColumnOption<DataRow>>();
        }

        /// <summary>
        /// 推入数据
        /// </summary>
        /// <param name="data"></param>
        /// <exception cref="NotImplementedException"></exception>
        public override void Put(object data)
        {
            if (data == null) throw new ArgumentNullException("数据源为空");
            if (!(data is DataTable)) throw new Exception("数据源不是DataTable类型");
            DataTable dt = (DataTable)data;
            DataSources = dt.Rows.Cast<DataRow>().ToList();
            //当没有为数据源配置时，则以数据源为标准执行数据导出
            if (ExcelColumns == null || !ExcelColumns.Any())
            {
                if (ExcelColumns == null) ExcelColumns = new List<ColumnOption<DataRow>>();
                foreach (DataColumn column in dt.Columns)
                {
                    ColumnOption<DataRow> temp = new ColumnOption<DataRow>(column.ColumnName);
                    ExcelColumns.Add(temp);
                }
            }
        }

        /// <summary>
        /// 设置每个单元格的值
        /// </summary>
        /// <param name="rowIndex"></param>
        /// <param name="row"></param>
        /// <param name="columnOption"></param>
        /// <returns></returns>
        public override object GetRowCell(int rowIndex, DataRow row, ColumnOption<DataRow> columnOption)
        {
            string val = string.Empty;
            if (!string.IsNullOrWhiteSpace(columnOption.FieldName) && row.Table.Columns.Contains(columnOption.FieldName))
            {
                if ((row[columnOption.FieldName] is DBNull)) return string.Empty;
                val = row[columnOption.FieldName].ToString();
            }
            else if (!string.IsNullOrWhiteSpace(columnOption.ColumnName) && row.Table.Columns.Contains(columnOption.ColumnName))
            {
                if ((row[columnOption.ColumnName] is DBNull)) return string.Empty;
                val = row[columnOption.ColumnName].ToString();
            }
            if (val == null) val = string.Empty;
            val = val.Trim();
            return val;
        }
    }
}
